package com.cyz.common.security;

import com.alibaba.fastjson.JSON;
import com.cyz.common.utils.R;
import com.cyz.common.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义授权失败处理器
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException){
        WebUtils.renderString(response, JSON.toJSONString(R.error(HttpStatus.FORBIDDEN.value(),"授权失败")));
    }
}
